import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//懒加载路由组件
const Login = () => import(/* webpackChunkName: "Login" */ "@/views/Login.vue") 
const Home = () => import(/* webpackChunkName: "Home" */ "@/views/Home.vue") 
const Dashbord = () => import(/* webpackChunkName: "Dashbord" */ "@/views/Dashbord.vue") 
const User = () => import(/* webpackChunkName: "User" */ "@/views/User.vue") 
const NotFound = () => import(/* webpackChunkName: "User" */ "@/views/404.vue") 

const routes = [
	{
		path: '/',
		redirect: '/login',
	},
	{
		path: '/login',
		name: 'Login',
		component: Login
	},
	{
		path: '/home',
		redirect: '/home/dashbord',
		name: 'Home',
		component: Home,
		children: [
			{
				path: '/home/dashbord',
				name: 'Dashbord',
				component: Dashbord
			},
			{
				path: '/home/users',
				name: 'User',
				component: User
			},
			{
				path: '*',
				name: 'NotFound',
				component: NotFound
			}
		]
	},
]

const router = new VueRouter({
  routes
})

//导航守卫
router.beforeEach((to, from, next) => {
	if(to.path !== '/login'){
		if( !window.sessionStorage.getItem('token') ){
			next('/login')
		}
	} 
 
	if(to.path === '/home/dashbord'){
		window.sessionStorage.setItem('current_index', '/home/dashbord')
	}
	
	next()
})

export default router
